package com.macro.mall.controller;

import com.macro.mall.annotationUtil.CurrentUserName;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.FilterParam;
import com.macro.mall.dto.SmsHomeAdvertiseDto;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.model.SmsHomeAdvertise;
import com.macro.mall.service.SmsHomeAdvertiseService;
import com.macro.mall.service.UmsAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页轮播广告管理Controller
 * Created by macro on 2018/11/7.
 */
@Controller
@Api(tags = "SmsHomeAdvertiseController", description = "首页轮播广告管理")
@RequestMapping("/home/advertise")
public class SmsHomeAdvertiseController {
    @Autowired
    private SmsHomeAdvertiseService advertiseService;

    @Autowired
    private UmsAdminService umsAdminService;

    /**
     * 添加广告：添加当前门店的广告
     * @author 高强
     * @Date 2020年8月29日
     * @param
     * @param advertise
     * @return
     */
    @ApiOperation("添加广告")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@CurrentUserName String currentUserName, @RequestBody SmsHomeAdvertise advertise) {
        UmsAdminDTO umsAdminDTO = umsAdminService.getAdminDTOByUserName(currentUserName);
        int count = advertiseService.create(umsAdminDTO.getStoreId(),advertise);
        if (count > 0){
            return CommonResult.success(count);
        }else if(count == -2){
            return CommonResult.failed("不能选择未通过审核的商品");
        }else{
            return CommonResult.failed();
        }
    }

    @ApiOperation("删除广告")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = advertiseService.delete(ids);
        if (count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

    @ApiOperation("修改上下线状态")
    @RequestMapping(value = "/update/status/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateStatus(@PathVariable Long id, Integer status) {
        int count = advertiseService.updateStatus(id, status);
        if (count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

    @ApiOperation("获取广告详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<SmsHomeAdvertiseDto> getItem(@PathVariable Long id) {
        SmsHomeAdvertiseDto advertise = advertiseService.getItem(id);
        return CommonResult.success(advertise);
    }

    @ApiOperation("修改广告")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody SmsHomeAdvertise advertise) {
        int count = advertiseService.update(id, advertise);
        if (count > 0){
            return CommonResult.success(count);
        }else if(count == -2){
            return CommonResult.failed("不能选择未通过审核的商品");
        }else{
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询广告")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<SmsHomeAdvertise>> list(@CurrentUserName String currentName,
                                                            @RequestBody FilterParam param) {
       UmsAdminDTO umsAdminDTO = umsAdminService.getAdminDTOByUserName(currentName);
        List<SmsHomeAdvertise> advertiseList = advertiseService.list(umsAdminDTO, param);
        return CommonResult.success(CommonPage.restPage(advertiseList));
    }
}
